# -*- coding: utf-8 -*-
import scrapy
from tencent.items import TencentItem

class Tencent01Spider(scrapy.Spider):
    name = 'tencent01'
    allowed_domains = ['hr.tencent.com']
    baseUrl = 'http://hr.tencent.com/position.php?&start='
    offset = 0
    limit = 10
    requestUrl = baseUrl+str(offset)
    start_urls = [requestUrl]
    detailPagePrefix = "http://hr.tencent.com/"

    def parse(self, response):
        # print(response.body.decode("utf-8"))
        # //tr[@class='even' or @class='odd']
        # //tr[@class='even'] | //tr[@class='odd']
        node_list = response.xpath("//tr[@class='even' or @class='odd']")
        if(not len(node_list)):
            print("抓拍完毕.\n")
            return
        for node in node_list:
            item = TencentItem()
            positionName = node.xpath("./td[1]/a/text()").extract()
            item['positionName'] = positionName[0] if len(positionName)  else ""
            positionLink =  node.xpath("./td[1]/a/@href").extract()
            item['positionLink'] = self.detailPagePrefix + positionLink[0] if len(positionLink)  else ""
            positionType = node.xpath("./td[2]/text()").extract()
            item['positionType'] = positionType[0] if len(positionType)  else ""
            positionNeedNum = node.xpath("./td[3]/text()").extract()
            item['positionNeedNum'] = positionNeedNum[0] if len(positionNeedNum)  else ""
            positionBase =  node.xpath("./td[4]/text()").extract()
            item['positionBase'] = positionBase[0] if len(positionBase)  else ""
            positionPublishTime =  node.xpath("./td[5]/text()").extract()
            item['positionPublishTime'] = positionPublishTime[0]  if len(positionPublishTime)  else ""
            yield item

        self.offset += self.limit
        self.requestUrl = self.baseUrl + str(self.offset)
        yield scrapy.Request(self.requestUrl,,callback=self.parse)
